package com.ymatou.query.seller.trading.api.parameter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;

/**
 * 存储过程：sp_trd_GetAppSellerOrders
 * @author songyefeng
 *
 */
public class Sp_GetAppSellerOrders {
	private int sellerId;
	private Integer orderSellerAcceptedStatus;
	private List<Integer> orderStatus;
	private int lastOrderId;
	private Boolean paidInFull;
	private String timeFrom;
	private String timeTo;
	private Integer top;
	@Deprecated
	private Boolean considerOrderStatus;
	@Deprecated
	private Boolean considerRCOrderEstablish;
	@Deprecated
	private Boolean considerRCAccountPaid;
	@Deprecated
	private Boolean considerRestOrderStatus;
	private int filter = 0;
	private String filterValue = null;

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public void setOrderSellerAcceptedStatus(Integer orderSellerAcceptedStatus) {
		this.orderSellerAcceptedStatus = orderSellerAcceptedStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public void setLastOrderId(Integer lastOrderId) {
		this.lastOrderId = lastOrderId;
	}

	public void setPaidInFull(Boolean paidInFull) {
		this.paidInFull = paidInFull;
	}

	public void setTimeFrom(String timeFrom) {
		this.timeFrom = timeFrom;
	}

	public void setTimeTo(String timeTo) {
		this.timeTo = timeTo;
	}

	public void setTop(Integer top) {
		this.top = top;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	public void setFilter(int filter) {
		this.filter = filter;
	}

	public void setFilterValue(String filterValue) {
		this.filterValue = filterValue;
	}

	public Sp_GetAppSellerOrdersByOrderIds execute() {
		YmtOrdersWapper ordersWapper = new YmtOrdersWapper();

		List<Integer> orderIds = null;
		int rowCount;

		String query = "iBuyerId = " + sellerId;
		StringBuffer sb = new StringBuffer();
		if (paidInFull != null || DataUtil.isNullOrEmpty(orderStatus) == false) {
			query += " and (";
			if (paidInFull != null) {
				sb.append(" (o.iTradingStatus = " + orderSellerAcceptedStatus
						+ " and o.bPaidInFull = "
						+ DataUtil.bool2Int(paidInFull) + ")");
			}
			if (DataUtil.isNullOrEmpty(orderStatus) == false) {
				if (sb.length() > 0) {
					sb.append(" or ");
				}
				sb.append(" o.iTradingStatus in "
						+ DataUtil.toInOfSql(orderStatus));
			}
			query += sb.toString()+" )";
		}
		if (timeFrom != null) {
			query += " and dAddTime >= '" + timeFrom + "' and dAddTime < '"
					+ timeTo + "'";
		}
		if (lastOrderId != 0) {
			query += " and iOrderId < " + lastOrderId;
		}
		switch (filter) {
		case 1:
			query += " and o.iOrderId like '%" + filterValue + "%'";
			break;
		case 2:
			query += " and o.sBuyerNickName like '%" + filterValue + "%'";
			break;
		case 3:
			query += " and o.sReceivePerson like '%" + filterValue + "%'";
			break;
		case 4:
			query += " and exists(select top 1 1 from Ymt_OrderSummary(nolock) where iOrderId = o.iOrderId and sSummary like '%"
					+ filterValue + "%')";
		}
		query += " order by iOrderId desc";
		List<Map> orders = ordersWapper.selectOrders2("top " + top
				+ " iOrderId", null, query);
		if (DataUtil.isNullOrEmpty(orders) == false) {
			orderIds = orders.stream().map(x -> (int) x.get("iOrderId"))
					.collect(Collectors.toList());
		}
		rowCount = orders.size();
		Sp_GetAppSellerOrdersByOrderIds sp = new Sp_GetAppSellerOrdersByOrderIds();
		sp.setOrderIds(orderIds);
		sp.setSellerId(sellerId);
		return sp.execute();
	}
}
